package com.mak001.lite.multiSkill.subs.MoneyMakers;

import org.rsbuddy.tabs.Inventory;
import org.rsbuddy.widgets.Bank;

import com.mak001.lite.multiSkill.Methods;
import extras.mak001.Time;
import com.mak001.lite.multiSkill.subScript;
import com.rsbuddy.event.events.MessageEvent;
import com.rsbuddy.script.methods.Npcs;
import com.rsbuddy.script.methods.Players;
import com.rsbuddy.script.methods.Walking;
import com.rsbuddy.script.methods.Widgets;
import com.rsbuddy.script.wrappers.Area;
import com.rsbuddy.script.wrappers.Tile;

public class WizardMindBomb extends subScript {

	private int amountToSpend;
	private int chooseWidget;
	private Area bankArea = new Area(new Tile(2942, 3367), new Tile(2950, 3374));
	private Area storeArea = new Area(new Tile(2953, 3368),
			new Tile(2960, 3375));
	private int[] tenders = { 736, 3217 };

	@Override
	public boolean canSwitch() {
		return true;
	}

	private enum state {
		BANK, WALKTOBANK, WALKTOSTORE, BUY, WITHDRAWGP
	}

	private state getState() {
		if (Inventory.isFull()) {
			if (bankArea.contains(Players.getLocal().getLocation())) {
				return state.BANK;
			} else {
				return state.WALKTOBANK;
			}
		} else {
			if (Inventory.getCount(true, 995) >= 81) {
				if (storeArea.contains(Players.getLocal().getLocation())) {
					return state.WALKTOSTORE;
				} else {
					return state.BUY;
				}
			} else {
				return state.WITHDRAWGP;
			}
		}
	}

	@Override
	public void execute() {
		if (amountToSpend > 3) {
			switch (getState()) {
			case BANK:
				if (Bank.isOpen()) {
					Bank.depositAll();
				} else {
					Bank.open();
				}
				break;
			case WALKTOBANK:
				Walking.findPath(bankArea.getCentralTile()).traverse();
				break;
			case WALKTOSTORE:
				Walking.findPath(storeArea.getCentralTile()).traverse();
				break;
			case WITHDRAWGP:
				if (Bank.isOpen()) {
					Bank.withdraw(995, 81);
				} else {
					Bank.open();
				}
				break;
			case BUY:
				if (Widgets.getComponent(241, 5).isValid()) {
					if (Widgets.getComponent(241, 5).click()) {
						sleep(500);
					}
				} else if (Widgets.getComponent(64, 5).isValid()) {
					if (Widgets.getComponent(64, 4).containsText("Mind Bomb")) {
						amountToSpend -= 3;
					}
					if (Widgets.getComponent(64, 5).click()) {
						sleep(500);
					}
				} else if (Widgets.getComponent(242, 6).isValid()) {
					if (Widgets.getComponent(242, 6).click()) {
						sleep(500);
					}
				} else if (Widgets.getComponent(232, 3).isValid()) {
					if (Widgets.getComponent(232, chooseWidget).click()) {
						sleep(500);
					}
				} else {
					if (Npcs.getNearest(tenders).interact("")) {
						sleep(500);
					}
				}
				break;

			}
		} else {
			Methods.stopSub();
		}
	}

	@Override
	public int[] required() {
		return null;
	}

	public void setDrink(String s) {
		if (s.contains("wizard")) {
			this.chooseWidget = 3;
		} else if (s.contains("ale")) {
			this.chooseWidget = 2;
		} else {// dwarven stout
			this.chooseWidget = 4;
		}
	}

	@Override
	public String getStatus() {
		if (getState() == state.BANK) {
			return "Banking";
		} else if (getState() == state.BUY) {
			return "Buying drink";
		} else if (getState() == state.WALKTOBANK) {
			return "Walking to bank";
		} else if (getState() == state.WALKTOSTORE) {
			return "Walking to bar";
		} else { //withdrawing gp
			return "Withdrawing money";
		}
	}

	@Override
	public int skill() {
		return 26;
	}

	@Override
	public void messageRecieved(MessageEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public String[] paintString() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Time getTime() {
		// TODO Auto-generated method stub
		return null;
	}

}
